/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:ces.org.course.event</p>
 * <p>File:FileUploadListener.java</p>
 * <p>类更新历史信息</p>
 *
 * @todo chanlong(陈龙) 创建于 May 17, 2019 4:24:21 PM
 */
package ces.org.course.api.event;

import ces.org.course.api.entity.DictionaryEntity;
import ces.org.course.api.service.DictionaryService;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Paths;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date May 17, 2019 4:24:21 PM
 * @version 1.0.2019
 */
@Slf4j
@Component
public class FileUploadListener {

    // 监听类型
    @Value("${ces.dict.trigger: auto}")
    private String trigger;

    @Autowired
    private DictionaryService service;

    /**
     * 使用 @EventListener 注解实现监听
     * 读取上传的文件, 解析xml, 更新到缓存
     * @param event
     */
    @EventListener
    public void listener(final FileUploadEvent event) {
        System.out.println(event.getMessage().getMessage());
        log.info("文件监听器: 触发模式 ==> {}", trigger);
        if (trigger.equals("event")) {
            String path = event.getMessage().getPath();
            if(StrUtil.isNotBlank(path)){
                File file = new File(path);
                if(file!=null&&file.isFile()&&"xml".equals(FileUtil.extName(file))){
                    service.put(DictionaryEntity.CACHE_KEY, file);
                    log.info("文件监听器: ==> 字典缓存已更新");
                }
            }
        }
    }
}
